<script lang="ts" setup>
import type { NotifyItem } from "./type"
import { Bell } from "@element-plus/icons-vue"
import { getMessagesList,updateMassageReadStatus,getMassageHistory} from "./data.ts"
import List from "./List.vue"
type TabName = "通知" | "消息" | "待办"
import {useUserStore} from "@/pinia/stores/user";
import {error} from "happy-dom/lib/PropertySymbol";
const userStore = useUserStore()
const userData = userStore.userInfo
interface DataItem {
  name: TabName
  type: "primary" | "success" | "warning" | "danger" | "info"
  list: NotifyItem[]
}
const notifyCount = ref(0)
const messageCount = ref(0)
const todoCount = ref(0)
const listData = ref([])
const getMessageData = async () => {
 await getMessagesList(userData).then((res) => {
   data.value[0].list = res.data.notifyData;   // 通知
   data.value[1].list = res.data.messageData;  // 消息
   data.value[2].list = res.data.todoData;     // 待办
   notifyCount.value = res.data.notifyCount;
   messageCount.value = res.data.messageCount;
   todoCount.value = res.data.todoCount;
 }).catch((err) => {

 })
}
getMessageData();

/** 角标当前值 */
// const badgeValue = computed(() => data.value.reduce((sum, item) => sum + item.list.length, 0))
/** 计算角标当前值 */
const badgeValue = computed(() => {
  return notifyCount.value + messageCount.value + todoCount.value
});
const isOpenHistory = ref(false)


/** 角标最大值 */
const badgeMax = 99

/** 面板宽度 */
const popoverWidth = 350

/** 当前 Tab */
const activeName = ref<TabName>("通知")

/** 所有数据 */
const data = ref([
  // 通知数据
  {
    name: "通知",
    type: "primary",
    list: []
  },
  // 消息数据
  {
    name: "消息",
    type: "danger",
    list: []
  },
  // 待办数据
  {
    name: "待办",
    type: "warning",
    list: []
  }
])
// 点击事件
const handClick = async (name) => {
  activeName.value = name
  let type=name==="通知"?0: name==="消息"?1:2;
  if (notifyCount.value !== 0 || messageCount.value !== 0 || todoCount.value !== 0){
    await updateMassageReadStatus({userId: userData.id, type: type }).then(res=>{
      if (res.code===0){
        type===0?notifyCount.value = 0:type===1?messageCount.value = 0:todoCount.value =0;
      }
    })
  }

}
const handClicks = (data) => {
  handClick(data);
  getMessageData();
  isOpenHistory.value=false;
}
// 历史事件
const handleHistory =async () =>{
  let type = 0;
  if (activeName.value === "消息"){
    //查询消息历史记录
    type=1
  }else if (activeName.value === "待办"){
    type=2
  }
  await getMassageHistory({userId: userData.id,type: type}).then(res=>{
    if (res.code===0){
      listData.value = res.data;
    }
  }).catch(error=>{

  })
  isOpenHistory.value = true
}
</script>

<template>
  <div class="notify">
    <el-popover placement="bottom" :width="popoverWidth" trigger="click">
      <template #reference>
        <el-badge :value="badgeValue"@click="handClicks('通知')" :max="badgeMax" :hidden="badgeValue === 0">
          <el-tooltip effect="dark" content="消息通知" placement="bottom">
            <el-icon :size="20">
              <Bell />
            </el-icon>
          </el-tooltip>
        </el-badge>
      </template>
      <template v-if="!isOpenHistory" #default>
        <el-tabs v-model="activeName" class="demo-tabs" stretch>
          <el-tab-pane v-for="(item, index) in data" :key="index" :name="item.name">
            <template @change="handClick(item.name)" #label>
              <span @click="handClick(item.name)" >{{ item.name }} </span>
              <el-badge v-if="item.name === '通知' && notifyCount!==0" :value="notifyCount" :max="badgeMax" :type="item.type" />
              <el-badge v-if="item.name === '消息' && messageCount!==0" :value="messageCount" :max="badgeMax" :type="item.type" />
              <el-badge v-if="item.name === '待办' && todoCount!==0" :value="todoCount" :max="badgeMax" :type="item.type" />
            </template>
            <el-scrollbar height="400px">
              <List :data="item.list" />
            </el-scrollbar>
          </el-tab-pane>
        </el-tabs>
        <div class="notify-history">
          <el-button link @click="handleHistory">
            查看{{ activeName }}历史
          </el-button>
        </div>
      </template>
      <template v-if="isOpenHistory" #default>
        <div class="notify-history-container">
          <div class="notify-history">{{activeName}}历史记录</div>
          <el-scrollbar height="400px">
            <List :data="listData" />
          </el-scrollbar>
          <div class="notify-history">
            <el-button link @click="isOpenHistory = false">
              关闭
            </el-button>
          </div>
        </div>
      </template>
    </el-popover>
  </div>
</template>

<style lang="scss" scoped>
.notify-history {
  text-align: center;
  padding-top: 12px;
  border-top: 1px solid var(--el-border-color);
}
.notify-history-container {
  height: 450px; /* 容器总高度 */
  overflow: hidden; /* 避免内容溢出 */
  display: flex;
  flex-direction: column;
}

.notify-history {
  margin-bottom: 8px;
  text-align: center;
  border-bottom: 1px solid var(--el-border-color);
}

.el-scrollbar {
  flex: 1; /* Scrollbar 占据剩余空间 */
}

</style>
